import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import login from '../views/login.vue'

import users from '../components/users'
import cate from '../components/product/cate.vue'
import params from '../components/product/params.vue'
import goods from '../components/product/goods.vue'
import goodsAdd from '../components/product/goodsAdd.vue'
import goodsUpdate from '../components/product/goodsUpdate.vue'
import reports from '../components/reports'
import roles from '../components/roles'
Vue.use(VueRouter)

const routes = [
  {
    path: '/home',
    name: 'home',
    component: HomeView,
    children: [{
      path: '/categories',
      name: 'cate',
      component: cate
    },
    {
      path: '/roles',
      name: 'roles',
      component: roles
    },
    // goods/update/929
    {
      path: '/goods/update',
      name: 'goodsUpdate',
      component: goodsUpdate
    },
    {
      path: '/reports',
      name: 'reports',
      component: reports
    },
    {
      path: '/users',
      name: 'users',
      component: users
    },
    {
      path: '/params',
      name: 'params',
      component: params
    },
    {
      path: '/goods',
      name: 'goods',
      component: goods
    },
    {
      path: '/goods/add',
      name: 'add',
      component: goodsAdd
    }]

  },
  {
    path: '/login',
    name: 'login',
    component: login
  },

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
//home    login
router.beforeEach((to, from, next) => {
  //to 当前页面信息   from  上个页面信息
  //如果是登录页面，就允许访问
  // to.path  /home
  if (to.path === '/login') return next()


  var token = sessionStorage.getItem("token"); //!null  true
  //如果不是登录页面，并且没有token,就跳转到登录页面
  if (!token) {//如果没有token,就说明没有登录，那么就跳转到登录页面
    return next("/login");
  }
  next();//只有调用这个反复，允许访问当前页面
})

export default router
